import { constantRoutes, asyncRoutes } from '@/router/index'
// console.log(constantRoutes)
export default {
  state: {
    routes: constantRoutes
  },
  mutations: {
    // 设置路由规则的mutation
    setRoutes(state, filterRoutes) {
      state.routes = [...constantRoutes, ...filterRoutes, { path: '*', redirect: '/404', hidden: true }]
    },
    // 重置规则为静态规则
    resetRoutes(state) {
      state.routes = [...constantRoutes]
    }
  },
  actions: {
    // action中所有的函数返回值默认都是promise
    filterRoutesAction({ commit }, roles) {
      const filterRoutes = asyncRoutes.filter(item => roles.menus.includes(item.children[0].name))
      commit('setRoutes', filterRoutes)
      return filterRoutes // 返回这个过滤好的数组, 是要给addRoutes方法添加规则的
    }

  },
  getters: {},
  namespaced: true
}
